
//
//  ZZEmoticonTools.swift
//  web
//
//  Created by 张卓 on 2017/2/11.
//  Copyright © 2017年 ZZ. All rights reserved.
//

import UIKit

//最大列数
let ZZEMOTICONMAXCOL = 7
//最大行数
let ZZEMOTICONMAXROW = 3
let ZZEMOTICONMAXCOUNT = ZZEMOTICONMAXCOL * ZZEMOTICONMAXROW - 1

class ZZEmoticonTools: NSObject {

    //全局访问点
    static let shared : ZZEmoticonTools = ZZEmoticonTools()
    
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last! as NSString).appendingPathComponent("recent.archiver")
    
    lazy var emoticonBundle:Bundle = {
        //路径
        let path = Bundle.main.path(forResource: "Emoticons.bundle", ofType: nil)!
        //获取boundle
        let bundle = Bundle(path: path)!
        return bundle
    }()
    //最近表情一维数组  最多20
    lazy var recentEmoticons: [ZZEmoticonModel] = {
//        return [ZZEmoticonModel]()
        return self.getRecentEmoticons()
    }()
    
  //默认表情一维数组
    
    lazy var normalEmoticons: [ZZEmoticonModel] = {
        return self.loadEmoticonArray(name:"default")
    }()
    //emoji表情
    lazy var emojiEmoticons: [ZZEmoticonModel] = {
        return self.loadEmoticonArray(name:"emoji")
    }()
    //xlh
    lazy var lxhEmoticons: [ZZEmoticonModel] = {
        return self.loadEmoticonArray(name:"lxh")
    }()
   
    
    //三维数组
    lazy var allEmoticons:[[[ZZEmoticonModel]]] = {
        return [
            [self.recentEmoticons],
            self.loadEmoticonGroupArray(emoticons: self.normalEmoticons),
            self.loadEmoticonGroupArray(emoticons: self.emojiEmoticons),
            self.loadEmoticonGroupArray(emoticons: self.lxhEmoticons)
        ]
    }()
    
}
//做最近表情的保存和获取的操作
extension ZZEmoticonTools {
    func saveRecentModel(emoticonModel: ZZEmoticonModel){
        //变量当前数组  去重
        for (i,model) in recentEmoticons.enumerated(){
            //判断是emoji还是
            if model.isEmoji {
                //emoji
                if model.code == emoticonModel.code {
                    recentEmoticons.remove(at: i)
                }
                
            }else{
                //图片表情
                if model.png == emoticonModel.png {
                    recentEmoticons.remove(at: i)
                }
                
            }
        }
        //添加最近表情数组中
        recentEmoticons.insert(emoticonModel, at: 0)
        //如果超过20个.删除最后一个
        if recentEmoticons.count > 20 {
            recentEmoticons.removeLast()
        }
        //三维数组中的最近表情的数组进行更改
        allEmoticons[0][0] = recentEmoticons
        //保存到沙盒中
        NSKeyedArchiver.archiveRootObject(recentEmoticons, toFile: file)
    }
    //从沙盒中获取是否有没有最近表情的数组
    func getRecentEmoticons()->[ZZEmoticonModel] {
        //判断沙盒中是否保存了
        if let emoticons = NSKeyedUnarchiver.unarchiveObject(withFile: file) as?[ZZEmoticonModel] {
            return emoticons
        }else {
            //第一次没有保存
            return [ZZEmoticonModel]()
        }
    }
}



extension ZZEmoticonTools {
    //通过该方法分别获取不同的表情包的一维数组
    func loadEmoticonArray(name: String) -> [ZZEmoticonModel]{
        let file = emoticonBundle.path(forResource: "\(name)/info.plist", ofType: nil)!
        let plistArray = NSArray(contentsOfFile: file)!
        //字典转模型
        let tempArray = NSArray.yy_modelArray(with: ZZEmoticonModel.self, json: plistArray) as! [ZZEmoticonModel]
        //遍历tempArr
        for model in tempArray {
            //给path赋值
            model.path = "\(name)" + "/" + "\(model.png ?? "")"
        }
        
        return tempArray
    }
    //通过该方法把一维数组转换成二维数组
    func loadEmoticonGroupArray(emoticons: [ZZEmoticonModel]) -> [[ZZEmoticonModel]]{
        //得到一维数组的页数
        
        let pageCount = (emoticons.count + ZZEMOTICONMAXCOUNT - 1)/ZZEMOTICONMAXCOUNT
        //再创建一个二维数组 可变空数组
        var groupArr:[[ZZEmoticonModel]] = [[ZZEmoticonModel]]()
        
        for i in 0..<pageCount{
            //位置
            let loc = i * ZZEMOTICONMAXCOUNT
            //长度
            var len = ZZEMOTICONMAXCOUNT
            // 防止越界
            if len + loc > emoticons.count{
                len = emoticons.count - loc
            }
            //范围
            let range = NSRange(location: loc, length: len)
            
            //截取数组
            let tempArr = (emoticons as NSArray).subarray(with: range) as! [ZZEmoticonModel]
            groupArr.append(tempArr)
        }
        return groupArr
        
    }
    
    
}
